import React from 'react'
import PropTypes from 'prop-types'
import { message } from 'antd'
import Component, { mapStateToProps, mapDispatchToProps } from '@components/Component'
import { connect } from 'react-redux'
import Approve from '@components/ApprovePre/Index'
import StepOneInput from './TransferTheFile/StepOneInput'
import StepOneHistory from './TransferTheFile/StepOneHistory'
// 调档
class TransferTheFiles extends Component {
  state = {
    dictionaries: {} // 枚举字典
  }

  // 获取枚举的字典值
  getDictionary () {
    this.$get('/api/xapc/dictionary', {
      types: '调档申请利用人身份类型,调用申请利用方式'
    }).then(res => {
      if (res.status === 1) {
        // 设置枚举字典
        const dictionaries = this.formatDictionaries(res.data)
        this.setState({
          dictionaries
        })
      } else {
        message.error(res.message)
      }
    })
  }

  // 格式化枚举字典
  formatDictionaries (dictionaries = []) {
    const result = {}
    dictionaries.forEach(d => {
      result[d.type] = d.list
    })
    return result
  }

  componentDidMount () {
    this.getDictionary()
  }

  renderCurrentStepLineTop (formRef, instance, renderFn) {
    switch (instance.currentStep.step_key) {
      case 'dangandiaoqu01':
        return <StepOneInput instance={instance} userInfo={this.props.userInfo} caseDetail={this.props.caseDetail} formRef={formRef} dictionaries={this.state.dictionaries} />
      default:
        return renderFn()
    }
  }

  // 渲染历史
  renderHistory (action, renderFn, instance, renderDocument) {
    const key = action.step_key
    const isDisagree = action.opt_add === 'disagree'
    if (key === 'dangandiaoqu01') {
      return <StepOneHistory key={action.id} userInfo={this.props.userInfo} instance={instance} action={action} />
    } else if (key === 'lian02' && !isDisagree) {
      // return <BusinessLeaderHistory key={action.id} userInfo={this.props.userInfo} instance={instance} action={action} />
    } else if (key === 'lian03') {
      // return <DocketDatetimeHistory key={action.id} userInfo={this.props.userInfo} instance={instance} action={action} renderDocument={renderDocument} />
    }
    return renderFn(action)
  }

  render () {
    return (
      <div>
        <Approve
          caseDetail={this.props.caseDetail}
          flowKey='dangandiaoqu'
          afterAction={(instance, action) => this.props.refreshCaseData()}
          creatable={this.props.editable}
          renderCurrentStepLineTop={(...params) => this.renderCurrentStepLineTop(...params)}
          renderHistory={(h, renderFn, instance, renderDocument) => this.renderHistory(h, renderFn, instance, renderDocument)}
          multiply
        />
      </div>
    )
  }
}

TransferTheFiles.propTypes = {
  caseDetail: PropTypes.object,
  // active: PropTypes.bool,
  // refreshCaseData: PropTypes.func
  editable: PropTypes.bool
  // fileList: PropTypes.array
  // getFileList: PropTypes.func
}
TransferTheFiles.defaultProps = {
  caseDetail: {},
  active: false,
  refreshCaseData: () => {},
  editable: false,
  fileList: [],
  getFileList: () => {}
}

const mapState = mapStateToProps(function (state) {
  return {
    userInfo: state.basic.userInfo
  }
})

const mapDispatch = mapDispatchToProps(function (dispatch) {
  return {

  }
})

export default connect(mapState, mapDispatch)(TransferTheFiles)
